import pyexiv2
import json
import datetime
import os
from shutil import copyfile

class Exif:
    def __init__(self, artist, copyright):
        self.artist    = artist
        self.copyright = copyright

    def tag(self, photo, debugInfo):
        metadata = pyexiv2.Image(photo)
        metadata.readMetadata()
        metadata['Exif.Image.DateTime']  = datetime.datetime.today()
        metadata['Exif.Image.Artist']    = self.artist
        metadata['Exif.Image.Copyright'] = copyright
        metadata['Exif.Photo.MakerNote'] = json.dumps(debugInfo)
        metadata.writeMetadata()

import unittest

class ExifTests(unittest.TestCase):
    loc = '/tmp/image.png'
    def setUp(self):
        f = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../t/fixtures/image.png')
        copyfile(f, self.loc)

    def test_tag(self):
        expectedDebugInfo = {'a':'b', 'c': 'd'}
        expectedAuthor = 'N'
        expectedCopyright = 'Copyright blah blah blah'
        exif = Exif(expectedAuthor, expectedCopyright)
        exif.tag(self.loc, expectedDebugInfo)
        metadata = pyexiv2.Image(self.loc)
        metadata.readMetadata()
        self.assertEqual( metadata['Exif.Image.Artist'], expectedAuthor)
        # doesn't work in 0.1.3, need 0.3.0 which is also an API change. need to determine
        # which version of Ubuntu is on Harris to know whether the new version will be easy to get
        #self.assertEqual( metadata['Exif.Photo.MakerNote'], json.dumps(expectedDebugInfo))

if __name__ == '__main__':
    unittest.main()
